home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / prolog_2.zip / PUZZLES.ZIP / VIENNEAU.PRO < prev    next >
Text File  |  1986-07-09  |  8KB  |  207 lines

  1. /*
  2.  
  3. This program solves the following logic puzzle.
  4.  
  5. Klare, Lemon, Morton, and Nelson are all women who love their work. They work
  6. as a dress designer, florist, gardener, and symphony conductor. Each woman has
  7. one and only only job, and each job is filled by one and only one woman. From
  8. the clues below, match up each woman's name with her type of work.
  9.  
  10.   1. Klare is violently allergic to most plants.
  11.  
  12.   2. Lemon and the florist are roommates.
  13.  
  14.   3. Morton and Lemon like only rock music.
  15.  
  16.   4. The gardener, the dress designer, and Nelson are strangers.
  17.  
  18.  
  19. This problem was an assignment in some AI class conducted at Syracuse
  20. University in the spring of 1986. (I didn't take the class.)
  21.  
  22. TO RUN, type
  23.               start.
  24.  
  25. */
  26.  
  27. /*  Program Author:
  28.  
  29.                  Robert L. Vienneau
  30.                  724 North James St
  31.                  Rome, NY 13440
  32.                  315-336-5417
  33.                                                           */
  34.  
  35. /*  The general approach is to write down everything I know and let the
  36. computer draw the conclusions. Question: would something like this be possible
  37. if I hadn't first solve the puzzle? It seems I first need to know what aspects
  38. of my knowledge of music, strangers, roommates, and loving-work is relevant.
  39. Then I have to know in what order to write down my knowledge. Both are
  40. crucial.
  41.  
  42. What does this say about the prospects for using this program as an exemplar
  43. for AI (considered as at attempt to build intelligent machines and expand our
  44. knowledge of workings of the human mind, not as productive of neat programming
  45. practices such as those involved in expert systems)?
  46.  
  47. Of course, in a sense, I have not specified any of my knowledge to the
  48. computer. The computer only "knows" syntactical rules for manipulating strings
  49. of symbols. The meaning of these symbols, their semantics/pragmatics, how they
  50. point to things in the world cannot be specified in a program. How we assign
  51. meaning to symbols is a deep philosophical problem. Those interested in more
  52. should read anything on Ludwig Wittegenstein (early or late periods).    */
  53.  
  54.  
  55.      /* Some stuff to help the user:  */
  56.  
  57. help :- start.
  58.  
  59. start :-
  60.  nl, print( 'Klare, Lemon, Morton, and Nelson are all women who love their' ),
  61.  nl, print( 'work.  They work as a dress designer, florist, gardener, and' ),
  62.  nl, print( 'symphony conductor. Each woman has one and only only job, and' ),
  63.  nl, print( 'each job is filled by one and only one woman. From the clues' ),
  64.  nl, print( 'below, match up each woman\'s name with her type of work.' ),
  65.  nl,
  66.  nl, print( '  1. Klare is violently allergic to most plants.' ),
  67.  nl,
  68.  nl, print( '  2. Lemon and the florist are roommates.' ),
  69.  nl,
  70.  nl, print( '  3. Morton and Lemon like only rock music.' ),
  71.  nl,
  72.  nl, print( '  4. The gardener, the dress designer, and Nelson are' ),
  73.  nl, print( '     strangers.' ),
  74.  nl,
  75.  nl,
  76.  nl, print( 'To find the solution to this problem, type such things as' ),
  77.  nl,
  78.  nl, print( ' job( \'symphony conductor\', nelson ).' ),
  79.  nl,
  80.  nl, print( 'Note types of work are first, enclosed in single quotes, and' ),
  81.  nl, print( 'uncapitalized; names are second and uncapitalized.' ).
  82.  
  83.  
  84.  
  85.          /*  Each woman has one and only one job. Each job is filled by
  86.              one and only one woman. (I believe if I coded these in all 
  87.              their generality I'd have infinite loops/recursion.)   */
  88.  
  89.          /*  I couldn't get the supplied not function to work so I wrote
  90.              my own (call ed knot)    */
  91.  
  92. job( 'florist', X ) :- woman( X ),
  93.              candidate( 'florist', X ),
  94.              knot( job( 'dress designer', X )),
  95.              knot( job( 'gardener', X )),
  96.              knot( job( 'symphony conductor', X )).
  97.  
  98. job( 'gardener', X ) :- woman( X ),
  99.              candidate( 'gardener', X ),
  100.              knot( job( 'dress designer', X )),
  101.              knot( candidate( 'florist', X )),
  102.              knot( job( 'symphony conductor', X )).
  103.  
  104. job( 'dress designer', X) :- woman( X ),
  105.              candidate( 'dress designer', X ),
  106.              knot( candidate( 'florist', X )),
  107.              knot( candidate( 'gardener', X )),
  108.              knot( job( 'symphony conductor', X )).
  109.  
  110. job( 'symphony conductor', X ) :- woman(X),
  111.              candidate( 'symphony conductor', X ),
  112.              knot( candidate( 'dress designer', X )),
  113.              knot( candidate( 'florist', X )),
  114.              knot( candidate( 'gardener', X )).
  115.  
  116.  
  117.          /* Some general facts about loving jobs, allegories, strangers,
  118.             roommates, rock an roll, etc.        */
  119.  
  120. candidate( X, Y ) :- work( X ), woman( Y ),
  121.                      firstcondition( X, Y ),
  122.                      secondcondition( X, Y ),
  123.                      thirdcondition( X, Y ),
  124.                      fourthcondition( X, Y ),
  125.                      fifthcondition( X, Y ).
  126.  
  127.            /* Those who love jobs as florists or gardeners are not
  128.               allergic to most plants  */
  129. firstcondition( 'dress designer', Y ) :- woman( Y ).
  130. firstcondition( 'florist', Y) :- woman( Y ), knot( allergic( Y ) ).
  131. firstcondition( 'gardener', Y) :- woman( Y ), knot( allergic( Y ) ).
  132. firstcondition( 'symphony conductor', Y ) :- woman( Y ).
  133.  
  134.           /* Those who love a job as a conductor do not like only rock  */
  135. secondcondition( 'dress designer', Y ) :- woman( Y ).
  136. secondcondition( 'florist', Y ) :- woman( Y ).
  137. secondcondition( 'gardener', Y ) :- woman( Y ).
  138. secondcondition( 'symphony conductor', Y ) :- woman( Y ),
  139.                              knot( likesonlyrock( Y )).
  140.  
  141.               /* In this puzzle, a person does not hold the same job 
  142.                  as her roommate  */
  143. thirdcondition( X, Y ) :- work( X ), woman( Y ),
  144.                           knot( roommate( X, Y )).
  145.  
  146.               /* In this puzzle, a person does not hold the same job 
  147.                  as people she's strangers with  */
  148. fourthcondition( X, Y ) :- work( X ), woman( Y ),
  149.                            knot( stranger1( X, Y )).
  150.  
  151.                /* The following code performs the following line of reasoning:
  152.                      One can show that lemon is either the dress designer
  153.                      or the gardener. nelson is strangers with both the
  154.                      dress designer and the gardener. Hence nelson and
  155.                      lemon are strangers. People are not strangers with
  156.                      their roommates; lemon's roomate is the florist.
  157.                      Hence nelson is not a candidate for the florist's
  158.                      job.                          */
  159.  
  160. fifthcondition( 'dress designer', X ) :- woman( X ).
  161. fifthcondition( 'gardener', X ) :- woman( X ).
  162. fifthcondition( 'symphony conductor', X ) :- woman( X ).
  163.      /* there is some bug I dont fully understand. It involves the eitherjob
  164.         function below calling candidate which calls fifthcondition which
  165.         calls eitherjob again. The next line is a patch that
  166.         takes care of it. More analysis should be done, though. */
  167. fifthcondition( 'florist', morton ).
  168. fifthcondition( 'florist', Y ) :- woman( Y ),
  169.                   knot(roommate('florist', Z)),
  170.                   knot(stranger3(Z, Y)).
  171. stranger3(X,Y) :- woman(X), woman(Y),
  172.                   eitherjob('dress designer', 'gardener', X),
  173.                   stranger1('dress designer', Y),
  174.                   stranger1('gardener', Y).
  175. eitherjob('dress designer', 'gardener', X) :- woman(X),
  176.                             knot(candidate('florist', X)),
  177.                             knot(candidate('symphony conductor', X)).
  178.  
  179.  
  180.         /* The specification of the puzzle in prolog follows */
  181.  
  182. allergic( klare ).
  183.  
  184. roommate( 'florist', lemon ).
  185.  
  186. likesonlyrock( morton ).
  187. likesonlyrock( lemon ).
  188.  
  189. stranger1( 'gardener', nelson).
  190. stranger1( 'dress designer', nelson).
  191.  
  192. woman( klare ).
  193. woman( lemon ).
  194. woman( morton ).
  195. woman( nelson ).
  196.  
  197. work( 'dress designer' ).
  198. work( 'florist' ).
  199. work( 'gardener' ).
  200. work( 'symphony conductor' ).
  201.  
  202.  
  203.    /* some useful functions  */
  204.  
  205. knot(P) :- P, !, fail.
  206. knot(P).
  207.